﻿@model Tuple<int?>
@{
    ViewData["Title"] = "设备运维";
}
 <div class="layui-fluid">
      <div class="layui-card">
           <div class="layui-form layui-card-header layuiadmin-card-header-auto search" lay-filter="app-content-list">
                <div class="layui-form-item"> 
                        <div class="layui-inline">
                            <label class="layui-form-label">巡检日期</label>
                            <div class="layui-input-inline">
                                <input type="text" name="startTime" class="layui-input date" id="test-laydate-start" placeholder="开始日期" lay-key="122">
                            </div>
                            <div class="layui-form-mid">
                                -
                            </div>
                            <div class="layui-input-inline">
                                <input type="text" name="endTime" class="layui-input date" id="test-laydate-end" placeholder="结束时间" lay-key="123">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <button class="layui-btn layuiadmin-btn-list" lay-submit="" lay-filter="search">
                                <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>查询
                            </button>
                            @if (1 != Model.Item1)
                            {
                                <button onclick="add()" class="layui-btn">添加巡检记录</button>
                            }

                        </div>
                    </div>
                </div>

                <div class="layui-card-body">                    
                    <table id="LAY-app-content-list" lay-filter="LAY-app-content-list"></table>
                </div>
            </div>
        </div>
   

<script type="text/javascript" src="~/js/deviceState.js"></script>
<script>
  

    var laydate = layui.laydate;
    var insStart = laydate.render({
        elem: '#test-laydate-start'
        , min: '2018-1-1'
        , done: function (value, date) {
            //更新结束日期的最小日期
            insEnd.config.min = lay.extend({}, date, {
                month: date.month - 1
            });

            //自动弹出结束日期的选择器
            insEnd.config.elem[0].focus();
        }
    });

    //结束日期
    var insEnd = laydate.render({
        elem: '#test-laydate-end'
        , min: '2018-1-1'
        , done: function (value, date) {
            //更新开始日期的最大日期
            insStart.config.max = lay.extend({}, date, {
                month: date.month - 1
            });
        }
    });

    form.render(null, 'app-content-list');

    //监听搜索
    form.on('submit(search)', function (data) {
        var field = data.field;
        //执行重载
        table.reload('LAY-app-content-list', {
            where: field
        });
    });
     

    //加载table
    var tableObj= table.render({
            elem: '#LAY-app-content-list'
            , url: '/checkLog/list/'  
            ,method: 'post'
            , height:"full-110"
            , page: true
            , cols: [[
                 
                 { field: 'name', title: '巡检日期', width: 180, templet: '<div>{{getDate(d.checkDate)}}</div>' }              
                , { field: 'name', title: '巡检人', width: 150, templet: '<div>{{getString(d.checkUser)}}</div>' }   
                , { field: 'name', title: '备注', templet: '<div>{{getString(d.remark)}}</div>' }   
                 
                , {
                    field: 'opr', title: '操作', width: 190, fixed:'right',
                templet:
                '<div class="layui-table-cell laytable-cell-11-0-8">'                
                    + ' {{ getButtons(d.id) }} '                    
                + '</div>'
            }
            ]]
            , skin: 'line'
        });

    function getContract(d) {
        if (d != null) return d.title;
        return "";
    }


    //根据状态获取不同的按钮
    function getButtons(id) {                  
        var html = ' <a class="layui-btn layui-btn-normal layui-btn-xs" onclick="edit(\'' + id +'\')"  ><i class="layui-icon layui-icon-edit"></i>编辑</a>'              
        html += '<a class="layui-btn layui-btn-danger layui-btn-xs" onclick="deleteItem(\'' + id + '\')"><i class="layui-icon layui-icon-delete"></i>删除</a> ';
        html += '<a class="layui-btn layui-btn-danger layui-btn-xs" onclick="viewDetail(\'' + id + '\')">查看</a> ';
        return html;      
    }   

    function deleteItem(id) {
        layer.confirm("确定要删除吗？", function (index) {
            layer.close(index);
            $.post("/checkLog/deleteItem?id=" + id,
                function (res) {
                    layer.msg(res.msg);
                    if (res.code == 1) {
                        tableObj.reload();
                    }
                });
        });
    }

    function add() {        
        winIndex = layer.open({
            type: 2,
            title: '添加巡检记录',
            content: '/checkLog/edit',
            area: ["1100px", "550px"]
        });
    }

    function edit(id) {
        winIndex = layer.open({
            type: 2,
            title: '编辑巡检记录',
            content: '/checkLog/edit?id='+id,
            area: ["1100px", "550px"]
        });
    }

    function viewDetail(id) {
        window.top.layer.open({
            type: 2,
            title: '查看',
            content: '/checkLog/detail?id=' + id,
            area: ["1100px", "550px"]
        });
    }

    var checkId = null;
    var tableDevice = null;
    var tableSelectDevice = null;
    var tableRoom = null;


    //添加设备到计划中
    function addDeviceCallback(deviceId) {
        layer.prompt({
            formType: 2,
            value: '',
            title: '请输入损坏设备的描述',
            area: ['400px', '250px'] //自定义文本域宽高
        }, function (value, index, elem) {
            layer.close(index);
            doAddBadDevice(deviceId, value);
        });
    }   

    function doAddBadDevice(deviceId,remark) {
        $.ajax({
            url: "/checkLog/AddDeviceToLog",
            type: 'post',
            data: {
                remark: remark,
                deviceId: deviceId,
                checkId: checkId
            },
            success: function (res) {
                layer.msg(res.msg);
                if (res.code == 1) {
                    tableDevice.reload();
                    tableSelectDevice.reload();
                }
            }
        });
    }      
    

</script>

